<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace addons\equip\user\controller;

use addons\equip\model\LockUp as LockUpM;

/**
 * 锁仓
 * Class LockUp
 * @package addons\equip\user\controller
 */
class LockUp extends \web\user\controller\AddonUserBase {

    public function index() {
        return $this->fetch();
    }

    /**
     * 结束锁仓
     * @return array|mixed|string
     */
    public function isEnd(){
        $id = $this->_post('id');
        $lockUpM = new LockUpM;
        $status = $lockUpM->getDetail($id,'status')['status'];
        try {
            if($status == 1) return $this->failData('当前已是结束状态，请勿重复提交');
            $res = $lockUpM->where(['id' => $id])->update(['status' => 1, 'update_time' => NOW_DATETIME]);
            if(!$res){
                return $this->failData('结束失败，请稍候再试');
            }
            //添加管理日志
            $this->addLog(self::LOG_EDIT, 'LockUp', [], $id, '结束锁仓');
            return $this->successData();

        } catch (\Exception $e) {
            return $this->failData($e->getMessage());
        }
    }

    /**
     * 锁仓列表
     * @return array
     */
    public function loadList() {
        $keyword = $this->_get('keyword');
        $filter = ' 1=1';
        if ($keyword != null) {
//            $filter .= ' and a.user_id like \'%' . $keyword . '%\' or equip_name like \'%' . $keyword . '%\' or username like \'%' . $keyword . '%\' or phone like \'%' . $keyword . '%\'';
             $filter .= ' and b.phone like \'%' . $keyword . '%\' or b.username like \'%' . $keyword . '%\' ';
        }
        $m = new LockUpM;
        $total = $m->getLockUpTotal($filter);
        $fields = 'a.*,b.username,b.phone';
        $rows = $m->getLockUpList($this->getPageIndex(), $this->getPageSize(), $filter, $fields);
        return $this->toDataGrid($total, $rows);
    }

    /**
     * 锁仓添加和修改
     * @return array|mixed|string
     */
    public function edit() {
        if (IS_POST) {
            $data = $_POST;
            $m = new LockUpM;
            try {
                if (empty($data['id'])) {
                    $data['create_time'] = NOW_DATETIME;
                    $data['remaining_amount'] = $data['amount'];

                    if($data['type'] == 2){
                        $accountM = new \addons\member\model\MemberAccountModel();

                        if($data['type'] == 2){ //代理锁仓才有
                            $data['balance_type'] = 2;
                            $pid = $accountM->getPID($data['user_id']);
                            if($pid > 0){
                                $pid_list = $m->where(['user_id' => $pid, 'type' => 2, 'd_number' => ['<', 10]])->select();
                                if($pid_list){
                                    foreach ($pid_list as $value){
                                        $update_data = [
                                            'release_tax' => $value['release_tax'] + 0.01,
                                            'd_number' => $value['d_number'] + 1,
                                            'update_time' => NOW_DATETIME,
                                        ];
                                        $m->where(['id' => $value['id']])->update($update_data);
                                    }
                                }
                            }
                        }
                    }
                    if($data['type'] == 3){
                        if($data['balance_type'] == 1){
                            return $this->failData('暂无USDT希望锁仓');
                        }
                    }
                    $ret = $m->add($data, false);
                } else {
                    $data['update_time'] = NOW_DATETIME;
                    $where['id'] = $data['id'];
                    $ret = $m->where($where)->update($data);
                }
                if(!$ret){
                    return $this->failData('提交失败');
                }

                //添加管理日志
                $this->addLog(self::LOG_EDIT, 'LockUp', $data, 0, '添加锁仓');
                return $this->successData();
            } catch (\Exception $e) {
                return $this->failData($e->getMessage());
            }
        } else {
            $id = $this->_get('id');
            $this->assign('id', $id);
            $this->setLoadDataAction('loadData');

            $list = (new \addons\member\model\MemberAccountModel())->field('id,username,phone')->where('logic_delete=0')->order('id asc')->select();
            $this->assign('user_list', json_encode($list, 256));
            return $this->fetch();
        }
    }

    /**
     * 修改
     * @return array|mixed|string
     */
    public function editor() {
        if (IS_POST) {
            $data = $_POST;
            $m = new LockUpM;
            try {
                if (empty($data['id'])) {
                    $data['create_time'] = NOW_DATETIME;
                    $data['remaining_amount'] = $data['amount'];

                    if($data['type'] != 1){
                        $data['balance_type'] = 2;
                    }

                    $ret = $m->add($data, false);
                } else {
                    $data['update_time'] = NOW_DATETIME;
                    $where['id'] = $data['id'];
                    $ret = $m->where($where)->update($data);
                }
                if(!$ret){
                    return $this->failData('提交失败');
                }

                //添加管理日志
                $this->addLog(self::LOG_EDIT, 'LockUp', $data, 0, '修改锁仓');
                return $this->successData();
            } catch (\Exception $e) {
                return $this->failData($e->getMessage());
            }
        } else {
            $id = $this->_get('id');
            $this->assign('id', $id);
            $this->setLoadDataAction('loadData');

            $list = (new \addons\member\model\MemberAccountModel())->field('id,username,phone')->where('logic_delete=0')->order('id asc')->select();
            $this->assign('user_list', json_encode($list, 256));
            return $this->fetch();
        }
    }


    public function loadData() {
        $id = $this->_get('id');
        $m = new LockUpM;
        $data = $m->getDetail($id);
        return $data;
    }
    /*
     * 补偿天数
     */
    public function add_days()
    {
        if (IS_POST){
            $orderM = new LockUpM;
            $days = $this->_get('days');
            $sql = 'UPDATE tp_equip_order  set end_time = DATE_ADD(end_time, INTERVAL '.$days.' DAY) ;';
            $res = $orderM->query($sql);
            if (!$res)return $this->successData();
            return $this->failData('更新失败');
        }else{
            $id = $this->_get('id');
            $this->assign('id', $id);
            return $this->fetch();
        }
    }
    /**
     * 删除
     */
    public function del() {
        $id = intval($this->_get('id'));
        if (!empty($id)) {
            $m = new LockUpM;
            try {
                $res = $m->deleteData($id);
                if ($res > 0) {
                    //添加管理日志
                    $this->addLog(self::LOG_DEL, 'LockUp', $id, 0, '删除锁仓');
                    return $this->successData();
                } else {
                    return $this->failData('删除失败');
                }
            } catch (\Exception $e) {
                return $this->failData($e->getMessage());
            }
        } else {
            return $this->failData('删除失败，参数有误');
        }
    }

}
